/*
 * Application.cpp
 *
 *  Created on: 2018年7月25日
 *      Author: terry
 */

#include "stdafx.h"
#include "Application.h"
#include "TSingleton.h"


typedef comn::Singleton< Application >		ApplicationSingleton;

Application& Application::instance()
{
	return ApplicationSingleton::instance();
}


Application::Application():
	m_cb(),
	m_user()
{
}

Application::~Application()
{
	teardownSerialPort();
}


int Application::init()
{
	return 0;
}

void Application::quit()
{
	teardownSerialPort();
}

void Application::setDisplayCallback(PoDisplayCallback cb, void* user)
{
	m_cb = cb;
	m_user = user;
}

void Application::display(int code, int value)
{
	if (m_cb)
	{
		(*m_cb)(code, value, m_user);
	}
}

void Application::setupSerialPort(const char* port, int baudrate)
{
	CString	str(port);
	try 
	{
		DWORD dwBaud = baudrate;
		m_serialPort.Open(str, dwBaud);
	}
	catch (...)
	{
		
	}
}

void Application::sendDataTo232(const char* data, int length)
{
	if (!m_serialPort.IsOpen())
	{
		return;
	}

	try
	{
		m_serialPort.Write(data, length);
	}
	catch (CSerialException& ex)
	{
		ex;
	}
}

void Application::teardownSerialPort()
{
	m_serialPort.Close();
}

void Application::demoRead()
{
	
	char buffer[MAX_PATH] = { 0 };
	DWORD size = m_serialPort.Read(buffer, MAX_PATH);
	if (size > 0)
	{
		OutputDebugStringA(buffer);
		//AtlTrace(TEXT("%s"), buffer);
	}
}